package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.RoleAction;
import com.eportal.domain.Sysopt;
import com.eportal.entity.SysrolesEntity;
import com.eportal.service.SysRolesService;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 角色管理
 * Created by Saber on 2017/8/18.
 */
@Controller
@RequestMapping(value = "/admin/role")
public class SysRoleController {

    private static Logger logger = LoggerFactory.getLogger(TOpenLogController.class.getName());

    @Resource
    private SysRolesService sysRolesService;

    /**
     * 角色管理列表接口
     * @author Saber
     * @Date 2017/8/18 下午5:30
     * @param request
     * @param pageEntity        分页对象
     * @param rname             角色名
     * @param rlevel            层级（0:平台  1:合作方公司   2:社区   3:锁厂）
     * @see RoleAction#page()
     * @return
     */
    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity,String rname,String rlevel){

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        JSONObject json = new JSONObject();

        try {
            PageInfo<SysrolesEntity> pageInfo=sysRolesService.queryForPage(pageEntity,rname,rlevel,user,userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }

    }
}
